package vn.allmobile.newsreader.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import vn.allmobile.newsreader.data.Category;
import vn.allmobile.newsreader.data.Paper;
import android.content.Context;

public class Utils {
	public static void CopyStream(InputStream is, OutputStream os) {
		final int buffer_size = 1024;
		try {
			byte[] bytes = new byte[buffer_size];
			for (;;) {
				int count = is.read(bytes, 0, buffer_size);
				if (count == -1)
					break;
				os.write(bytes, 0, count);
			}
		} catch (Exception ex) {
		}
	}

	public static List<Paper> getPapers(Context context) {
		List<Paper> arrPapers = new ArrayList<Paper>();

		StringBuilder sb = null;
		// String response =
		// "{\"0\":{\"title\":\"24h\",\"img\":\"http:\/\/static-m.24hstatic.com\/templates\/24hmobile\/images\/banner.gif\",\"category\":[{\"tintuctrongngay\":{\"title\":\"Tin
		// T\u1ee9c Trong
		// Ng\u00e0y\",\"url\":\"http:\/\/www.24h.com.vn\/upload\/rss\/tintuctrongngay.rss\"}},{\"bongda\":{\"title\":\"B\u00f3ng
		// \u0111\u00e1\",\"url\":\"http:\/\/www.24h.com.vn\/upload\/rss\/bongda.rss\"}},{\"anninh-hinhsu\":{\"title\":\"An
		// ninh - H\u00ecnh
		// s\u1ef1\",\"url\":\"http:\/\/www.24h.com.vn\/upload\/rss\/anninhhinhsu.rss\"}},{\"thoitrang\":{\"title\":\"Th\u1eddi
		// trang\",\"url\":\"http:\/\/www.24h.com.vn\/upload\/rss\/thoitranghitech.rss\"}},{\"tienvang-chungkhoan\":{\"title\":\"Ti\u1ec1n-Va\u0300ng-Ch\u01b0\u0301ng
		// khoa\u0301n\",\"url\":\"http:\/\/www.24h.com.vn\/upload\/rss\/tienvangchungkhoan.rss\"}},{\"Phim\":{\"title\":\"Phim\",\"url\":\"http:\/\/www.24h.com.vn\/upload\/rss\/phim.rss\"}}]},\"1\":{\"title\":\"VnExpress\",\"img\":\"http:\/\/st.f1.vnexpress.net\/images\/logo.png\",\"category\":[{\"trangchu\":{\"title\":\"Trang
		// ch\u1ee7\",\"url\":\"http:\/\/vnexpress.net\/rss\/gl\/trang-chu.rss\"}},{\"xahoi\":{\"title\":\"X\u00e3
		// h\u1ed9i\",\"url\":\"http:\/\/vnexpress.net\/rss\/gl\/xa-hoi.rss\"}},{\"thegioi\":{\"title\":\"Th\u1ebf
		// gi\u1edbi\",\"url\":\"http:\/\/vnexpress.net\/rss\/gl\/the-gioi.rss\"}},{\"kinhdoanh\":{\"title\":\"Kinh
		// doanh\",\"url\":\"http:\/\/vnexpress.net\/rss\/gl\/kinh-doanh.rss\"}}]},\"2\":{\"title\":\"CAND\",\"img\":\"http:\/\/m.congan.com.vn\/images\/logo.gif\"},\"category\":[{\"datnuoc-connguoi\":{\"title\":\"\u0110\u1ea5t
		// n\u01b0\u1edbc - con
		// ng\u01b0\u1eddi\",\"url\":\"http:\/\/congan.com.vn\/vie\/rss\/rss.php?catid=880\"}},{\"amthuc\":{\"title\":\"\u1ea8m
		// th\u1ef1c\",\"url\":\"http:\/\/congan.com.vn\/vie\/rss\/rss.php?catid=442\"}},{\"antoan-cuocsong\":{\"title\":\"An
		// to\u00e0n cu\u1ed9c
		// s\u1ed1ng\",\"url\":\"http:\/\/congan.com.vn\/vie\/rss\/rss.php?catid=706\"}}]}";
		try {
			InputStream papers = context.getAssets().open("papers.json");
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					papers));
			sb = new StringBuilder();
			String line = null;
			try {
				while ((line = reader.readLine()) != null) {
					sb.append(line + "\n");
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					papers.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			JSONArray jaPapers = new JSONArray(sb.toString());
			for (int i = 0; i < jaPapers.length(); i++) {
				JSONObject joPaper = jaPapers.getJSONObject(i);
				// JSONArray jaPaper = jaPapers.getJSONArray(i);
				String title = joPaper.getString("title");
				String img = joPaper.getString("img");
				String category = joPaper.getString("category");
				// Log.v("title:" + title + "\t"+"img:" + img + "category:" +
				// category +"\n");
				// Log.v("-----------------title:" + title + "\t"+"img" + img
				// +"\n");

				JSONObject joarrCategory = new JSONObject(category);
				// Log.v(joarrCategory.keys().toString());

				HashMap<String, Category> arrCategory = new HashMap<String, Category>();

				Iterator iter = joarrCategory.keys();
				while (iter.hasNext()) {
					String key = (String) iter.next();
					JSONObject joCategory = joarrCategory.getJSONObject(key);
					String cat_title = joCategory.getString("title");
					String cat_url = joCategory.getString("url");
					Category cat = new Category(key, cat_title, cat_url);
					arrCategory.put(key, cat);
					// Log.v("\t cat_title:" + cat_title + "\t"+"cat_url" +
					// cat_url + "\n");
				}
				// JSONArray jaarrCategory = new JSONArray("[" + category +
				// "]");
				/*
				 * for(int j = 0; j < joarrCategory.length(); j ++) { JSONObject
				 * joCategory = joarrCategory.getJSONObject(); String cat_title
				 * = joCategory.getString("title"); String cat_url =
				 * joCategory.getString("url"); Category cat = new
				 * Category(cat_title, cat_url);
				 * arrCategory.put(String.valueOf(j), cat);
				 * Log.v("\t cat_title:" + cat_title + "\t"+"cat_url" + cat_url
				 * + "\n"); //arrCategory. }
				 */
				Paper paper = new Paper(title, img, arrCategory);
				arrPapers.add(paper);

				// arrPapers

			}
			return arrPapers;

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return arrPapers;
	}
}